//
//  StationDetailController.m
//  AirQuality
//
//  Created by Mac on 16/1/22.
//  Copyright © 2016年 Po. All rights reserved.
//

#import "StationDetailController.h"
#import "StationDetailModel.h"
#import "TopBackgroundImage.h"
#import "StationDetailView.h"
#import "RJBarView.h"
#import "HistogramView.h"
#import "RJHomeTopSectionView.h"

@interface StationDetailController ()<HistogramViewDelegate,
                                    RJHomeTopSectionViewDelegate>

- (void)initData;       //数据初始化
- (void)initUserInterface;  //初始化界面
@property (strong, nonatomic) TopBackgroundImage * topImage;   //上方背景图
@property (strong, nonatomic) StationDetailView * detailView;  //详情主视图
@property (retain, nonatomic) RJBarView * myBarView;
@property (retain, nonatomic) UIView * last24HourSection;              //最近24小时
@property (strong, nonatomic) HistogramView * histogramView; //柱状图
@property (strong, nonatomic) StationDetailModel * model;  //模型
@property (strong, nonatomic) NSDictionary * oneMonthDic; //一个月数据
@property (strong, nonatomic) NSDictionary * oneYearDic; //一年数据
@property (retain, nonatomic) RJHomeTopSectionView * segmentView;          //分段控制器
@property (retain, nonatomic) RJLoadView * myloadView;
@property (assign) NSInteger allData;

@end

@implementation StationDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self.view addSubview:[self getLoadView]];
    [self initUserInterface];
}

- (void)initData
{
    _allData = 0;
    _oneMonthDic = [NSDictionary dictionary];
    _oneYearDic = [NSDictionary dictionary];
    _model = [[StationDetailModel alloc] initWith:_topDic[@"STATIONCODE"]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOneMothData:) name:@"OneMonthData" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOneYearData:) name:@"OneYearData" object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"OneMonthData" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"OneYearData" object:nil];
}

- (void)initUserInterface
{
    [self.navBar setReturnButtonHidden:NO];
    self.navBar.centerCityLabel.text = _topDic[@"STATIONNAME"];
    [self.view addSubview:[self getDetailView]];
    [_detailView.detailScrollView addSubview:[self getTopBankgroundImage]];
    [_detailView.detailScrollView addSubview:[self getBarView]];
    [_detailView.detailScrollView addSubview:[self getSegmentedControl]];
}

- (TopBackgroundImage *)getTopBankgroundImage
{
    if (!_topImage) {
        _topImage = [[TopBackgroundImage alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 180)];
        _topImage.value = _topDic[@"AQI"];
    }
    return _topImage;
}

- (StationDetailView *)getDetailView
{
    if (!_detailView) {
        _detailView = [[StationDetailView alloc] initWithFrame:CGRectMake(0, sysNavBarHeight, ScreenWidth, ScreenHeight-sysNavBarHeight)];
    }
    return _detailView;
}


- (RJBarView *)getBarView
{
    if (!_myBarView) {
        CGRect rect = CGRectMake(0, 220, ScreenWidth, 115);
        _myBarView = [[RJBarView alloc] initWithFrame:rect data:_topDic];
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, _myBarView.frame.size.height-2, ScreenWidth, 2)];
        lineView.backgroundColor = SysMainColor;
        [_myBarView addSubview:lineView];
    }
    return _myBarView;
}

- (HistogramView *)getHistogramView
{
    if (!_histogramView) {
        _histogramView = [[HistogramView alloc] initWithFrame:CGRectMake(0, 375, ScreenWidth, 200) withData:_oneMonthDic withDayArr:_oneYearDic];
        _histogramView.delegate = self;
    }
    return _histogramView;
}

- (RJHomeTopSectionView *)getSegmentedControl
{
    if (!_segmentView) {
        NSArray * array = @[@"AQI",@"SO₂",@"NO₂",@"CO",@"O₃",@"PM₁₀",@"PM"];
        _segmentView = [[RJHomeTopSectionView alloc] initWithFrame:CGRectMake(0, 575-10, ScreenWidth, 35) titles:array];
        [_segmentView setSelectCount:0];
        [_segmentView setDelegate:self];
    }
    return _segmentView;
}

#pragma mark - RJHomeTopSectionViewDelegate
- (BOOL)HomeTopSectionView:(RJHomeTopSectionView *)sectionView didSelectCount:(NSUInteger)count isLeft:(BOOL)isLeft
{
    [_histogramView removeFromSuperview];
    _histogramView = nil;
    NSArray * typeArray = @[@"AQI",@"SO2",@"NO2",@"CO",@"O3",@"PM10",@"PM2_5"];
    NSString * type = typeArray[count];
    _oneYearDic = [_model get365dataWithType:type];
    _oneMonthDic = [_model get30DataWithType:type];
    [_detailView.detailScrollView addSubview:[self getHistogramView]];
    return YES;
}
- (void)isChange:(BOOL)change withKey:(NSString *)key
{
//    [_histogramView removeFromSuperview];
//    if (change) {
//     [_detailView.detailScrollView addSubview:[self getHistogramView]];
//    }
}

- (void)getOneMothData:(NSNotification *)notification
{
    NSDictionary * userInfo = notification.userInfo;
    if (userInfo[@"result"]) {
         _allData ++;
       _oneMonthDic = [_model get30DataWithType:@"AQI"];
        if (_allData == 4) {
            [_detailView.detailScrollView addSubview:[self getHistogramView]];
            [self removeLoadView];
        }
    }
}

- (void)getOneYearData:(NSNotification *)notification
{
    NSDictionary * userInfo = notification.userInfo;
    if (userInfo[@"result"]) {
        _allData ++;
        _oneYearDic = [_model get365dataWithType:@"AQI"];
        if (_allData == 4) {
            [_detailView.detailScrollView addSubview:[self getHistogramView]];
            [self removeLoadView];
        }
    }
}

- (RJLoadView *)getLoadView
{
    if (!_myloadView) {
        _myloadView = [[RJLoadView alloc] initWithFrame:self.view.bounds];
        self.view.userInteractionEnabled = NO;
        self.navBar.userInteractionEnabled = YES;
    }
    return _myloadView;
}

- (void)removeLoadView
{
    if (_myloadView) {
        [_myloadView removeSelf];
        _myloadView = nil;
         self.view.userInteractionEnabled = YES;
    }
}
@end
